package zt.song.shopping.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import zt.song.shopping.domain.Goods;
import zt.song.shopping.domain.OrderItem;
import zt.song.shopping.domain.Orders;
import zt.song.shopping.domain.ShoppingCart;
import zt.song.shopping.domain.vo.GoodsVo;
import zt.song.shopping.mapper.OrderItemMapper;
import zt.song.shopping.mapper.OrdersMapper;
import zt.song.shopping.service.GoodsService;
import zt.song.shopping.mapper.GoodsMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author 宋伟宁
* @description 针对表【t_goods】的数据库操作Service实现
* @createDate 2023-12-06 10:40:51
*/
@Service
@Transactional
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
    implements GoodsService{

    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private OrderItemMapper orderItemMapper;


    @Override
    public boolean pay(int userid,Integer total, List<ShoppingCart> shoppingCart) {
        int m=0;
        Orders orders = new Orders();
        String ordernum=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        orders.setOrdernum(ordernum);
        orders.setStatus(0);
        orders.setOrdertime(new Date());
        orders.setUserid(userid);
        orders.setTotalprice(new BigDecimal(total));
        //保存订单表
        m=this.ordersMapper.insert(orders);

        OrderItem item = null;
         for(ShoppingCart cart: shoppingCart) {
             Goods goods = cart.getGoods();
             item = new OrderItem();
             item.setGid(goods.getGid());
             item.setGamount(cart.getCount());
             item.setOrdernum(ordernum);
             m+=this.orderItemMapper.insert(item);
         }
        return m>=2;
    }

    @Override
    public List<GoodsVo> showHistory(int userid) {
        return this.ordersMapper.showHistory(userid);
    }
}




